package org.datacoder.geocoding.datastructures.geoshapes

import com.vividsolutions.jts.geom.*
import com.vividsolutions.jts.geom.impl.*
import org.datacoder.geocoding.*

/**
 * The bounding rectangle for a jurisdiction.
 */
class GeoRectangle extends GeoShape {

    public double lon1
    public double lon2
    public double lat1
    public double lat2

    public retrieveMinValue(int type){
        if(type == LON){
            return lon1
        }else{
            return lat1
        }
    }
    public retrieveMaxValue(int type){
        if(type == LON){
            return lon2
        }else{
            return lat2
        }
    }
    public double retrieveCenter(int type){
        if(type == LON){
            return (lon1 + lon2)/2
        }else{
            return (lat1 + lat2)/2
        }
    }
    public boolean containsInterval(int type, double min, double max){
        if(type == LON){
            return (lon1 <= min) && (max <= lon2)
        }else{
            return (lat1 <= min) && (max <= lat2)
        }
    }
    public boolean containsGeoPoint(GeoPoint gp){
        if((lon1 <= gp.lon) && (gp.lon <= lon2) &&
            (lat1 <= gp.lat) && (gp.lat <= lat2)){
            return true
        }else{
            return false
        }
    }

    public GeoInterval toGeoInterval(int type){
        if(type == LON){
            return new GeoHzInterval(
                lon1:lon1,
                lon2:lon2,
                lat:lat1
            )
        }else{
            return new GeoVtInterval(
                lon:lon1,
                lat1:lat1,
                lat2:lat2
            )
        }
    }
    public Geometry toGeometry(){
        Coordinate[] corners = new Coordinate[5]
        corners[0] = new Coordinate(lon1, lat2)
        corners[1] = new Coordinate(lon2, lat2)
        corners[2] = new Coordinate(lon2, lat1)
        corners[3] = new Coordinate(lon1, lat1)
        corners[4] = corners[0]
        Polygon rectangle = new Polygon(
            new LinearRing(new CoordinateArraySequence(corners), GeocodingUtils.geometryFactory),
            null,
            GeocodingUtils.geometryFactory
        )
        return rectangle
    }

}

